package com.wujiansima.renyisima.demo.demo4;

import sun.misc.Lock;

import java.util.concurrent.locks.ReentrantLock;

public class MyTicket implements Runnable{
    private int ticket = 100;
    private Object object = new Object();
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
//            synchronized (object){
            lock.lock();
                if (ticket <= 0){
                    break;
                }else {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
                        lock.unlock();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在出票，还剩余" + --ticket + "张票");
                }

        }

    }
}
